# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from tortoise import fields
from kernels.model import DbModel


class RefundRecordModel(DbModel):
    id = fields.IntField(pk=True, unsigned=True, description="主键")
    admin_id = fields.IntField(null=False, default=0, description="关联管理")
    user_id = fields.IntField(null=False, default=0, description="关联用户")
    order_id = fields.IntField(null=False, default=0, description="关联订单")
    order_type = fields.CharField(null=False, default="", max_length=20, description="订单类型: [order,recharge]")
    refund_type = fields.CharField(null=False, default="", max_length=20, description="退款类型: [back=后台,user=用户]")
    refund_sn = fields.CharField(null=False, default="", max_length=64, description="退款编号")
    order_sn = fields.CharField(null=False, default="", max_length=64, description="订单编号")
    transaction_id = fields.CharField(null=False, default=0, max_length=200, description="交易单号")
    total_amount = fields.DecimalField(null=False, default=0, max_digits=10, decimal_places=2, description="订单总额")
    refund_amount = fields.DecimalField(null=False, default=0, max_digits=10, decimal_places=2, description="退款金额")
    refund_status = fields.SmallIntField(null=False, default=0, description="退款状态: [0=退款中, 1=成功, 2=失败]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")

    class Meta:
        table_description = "退款记录表"
        table = DbModel.table_prefix("refund_record")


class SquareCategoryModel(DbModel):
    id = fields.IntField(pk=True, unsigned=True, description="主键")
    scene = fields.CharField(null=False, default="", max_length=20, description="场景: [robot,draws,video]")
    name = fields.CharField(null=False, max_length=100, default="", description="分类名称")
    icon = fields.CharField(null=False, max_length=250, default="", description="分类名称")
    sort = fields.IntField(null=False, default=0, description="排序编号")
    is_disable = fields.SmallIntField(null=False, default=0, description="是否禁用: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "广场分类表"
        table = DbModel.table_prefix("square_category")

    @classmethod
    async def queryCateDictsById(cls, ids: list, field: list = None) -> dict:
        """ 获取分类字典 """
        dicts = {}
        if ids:
            field = ["id", "scene", "name", "icon", "sort"] if not field else field
            users = await (cls.filter(id__in=list(set(ids)))
                           .filter(is_delete=0)
                           .all().values(*field))
            dicts = {item["id"]: item for item in users}
        return dicts
